package net.net23.sysdent;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class FullScreenWindow extends JFrame {

	private static final long serialVersionUID = -7294949741372637868L;
	private GraphicsDevice device;
	private boolean fullscreen;

	public FullScreenWindow(boolean initFullScreen) {
		device = GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getDefaultScreenDevice();
		this.setFullscreen(initFullScreen);
		if (initFullScreen) {
			setUndecorated(true);
		}
		changeMode(initFullScreen);
		continueInitialize();
	}

	private void continueInitialize() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				closeWindow();
			}
		});
	}

	void closeWindow() {
		System.exit(0);
	}

	void changeMode(boolean fullscreen) {
		this.setFullscreen(fullscreen);
		if (fullscreen) {
			device.setFullScreenWindow(this);
		} else {
			setSize(640, 480);
			device.setFullScreenWindow(null);
		}

	}

	public void setFullscreen(boolean fullscreen) {
		this.fullscreen = fullscreen;
	}

	public boolean isFullscreen() {
		return fullscreen;
	}
}
